package com.bidepan.big_event.controller;

import com.bidepan.big_event.pojo.Category;
import com.bidepan.big_event.pojo.Result;
import com.bidepan.big_event.service.CategoryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success("添加文章分类成功");
    }

    //当前用户创建的所有分类
    @GetMapping
    public Result<List<Category>> listCategory(){
        List<Category> categoryList = categoryService.listCategory();
        return Result.success(categoryList);
    }

    //分类详情
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category category = categoryService.findById(id);
        return Result.success(category);
    }

    //详情页中更新分类
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success("修改文章分类成功");
    }


    //删除文章分类
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id){
        categoryService.delete(id);
        return Result.success("删除文章分类成功");
    }
}
